// pages/tabBar/my/component/admin/admin.js
import { request } from '../../../../../utils/util'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userInfo: {},//保存用户信息
    value: "",    //搜索内容
    class: {            //清楚按钮的样式控制
      clear: "clear"
    },
    defaltAvat: "/images/icon/icon.png",//未有微信头像时的默认头像
    searchResult: {},
    isTap: false
  },

  /**
   * 生命周期函数--监听页面加载
   * 获取用户信息
   */
  onLoad: function (options) {
    const that = this
    wx.getStorage({
      key: 'userInfo',
      success(res) {
        that.setData({
          ...that.data,
          userInfo: res.data
        })
      }
    })
  },


  /**
   * 用户点击右上角分享
   * 邀请人员
   */
  onShareAppMessage: function (res) {
    if (res.from === 'button') {
      // 来自页面内转发按钮

    }
    return {
      title: `${this.data.userInfo.first_name}邀请你加入${this.data.userInfo.org_name}`,
      path: `/pages/tabBar/my/component/admin/components/invitePage/invitePage?org=${this.data.userInfo.org}&name=${this.data.userInfo.first_name}&org_name=${this.data.userInfo.org_name}`,
      imageUrl: '/images/icon/invite53.png'
    }
  },
  /**
   * 清除
   */
  clear: function () {
    this.setData({
      ...this.data,
      value: "",
      class: {
        clear: "clear"
      },
      searchResult: {}
    })
  },
  /**
   * 搜索
   */
  doSearch: function () {
    this.search();
  },
  /**
   * 搜索的方法
   */
  search: function () {
    const that = this
    const param = {
      phone: this.data.value
    }
    request("GET", "/api/auth/query_user/", param, "", true, "查询中")
      .then(res => {
        if (res.statusCode === 200) {
         

          if (res.data.length === 0) {
            wx.showToast({
              title: '该用户未注册',
              icon: "none"
            })
          } else if (res.data.code === '202') {
            wx.showToast({
              title: '该用户已存在本公司',
              icon: "none"
            })
          } else {
            that.setData({
              ...that.data,
              searchResult: res.data[0]
            })
          }
        }
      })
  },
  /**
   * 键盘输入事件
   */
  input: function (e) {
    this.setData({
      ...this.data,
      value: e.detail.value
    })
    if (e.detail.value.length > 0) {
      this.setData({
        ...this.data,
        class: {
          clear: "clear-show"
        }
      })
    } else {
      this.setData({
        class: {
          clear: "clear"
        }
      })
    }
  },

  /**
   * 邀请
   */
  invite() {
    const that = this
    const userInfo = this.data.userInfo
    const param = {
      org: userInfo.org,
      sender: userInfo.id,
      receiver: this.data.searchResult.id,
      submit_action: "邀请",
      social_type: "微信",
      message: "邀请您加入",
      phone: this.data.searchResult.phone
    }
    request("POST", "/api/auth/message/", param, "", true, "发送中")
      .then(res => {
        if (res.statusCode === 201) {
          that.setData({
            ...that.data,
            isTap: true
          })
        }
      })
  },
  toolTip() {
    wx.showModal({
      showCancel:"false",
      content: "通过手机号邀请已经处于其他公司的用户加入本公司",
      success(res) {
        if (res.confirm){

        }
      }
    })
  }

})